package com.insurance.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.insurance.bean.Company;
import com.insurance.service.CompanyService;


@Transactional
public class CompanyServiceBean implements CompanyService {
	@Resource private SessionFactory sessionFactory;

	public void save(Company company){
		sessionFactory.getCurrentSession().persist(company);
	}
	
	public void update(Company company){
		sessionFactory.getCurrentSession().merge(company);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Company getCompany(Integer companyid){
		return (Company)sessionFactory.getCurrentSession().get(Company.class, companyid);
	}

	public void delete(Integer companyid){
		sessionFactory.getCurrentSession().delete(
				sessionFactory.getCurrentSession().load(Company.class, companyid));
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Company> getCompanys(){		
		return sessionFactory.getCurrentSession().createQuery("from Company").list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Company> getCompanysTop10(){		
		return sessionFactory.getCurrentSession().createQuery("from Company order by id desc").
		setFirstResult(0).setMaxResults(10).list();
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@SuppressWarnings("unchecked")
	public List<Company> getCompanysTop5(){		
		return sessionFactory.getCurrentSession().createQuery("from Company order by id desc").
		setFirstResult(0).setMaxResults(5).list();
	}
}
